package org.shop.controller.bbsManager;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Merchant;
import org.shop.entity.SystemMessage;
import org.shop.entity.Users;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.MerchantService;
import org.shop.service.SystemMessageService;
import org.shop.service.UserService;
import org.shop.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class bbsManagerUserController {

	private static Logger logger = LogManager.getLogger(bbsManagerUserController.class);
	
	@Autowired
    private UserService userService;
	@Autowired
	private SystemMessageService systemMessageService;
	@Autowired
	private MerchantService merchantService;
	
	
	/** 
	* @Title: getAllUserInfoList 
	* @Description: TODO(条件获取用户信息) 
	* @param @param req
	* @param @return     
	* @return Map<String,Object>    返回类型 
	* @throws 
	*/
	@RequestMapping("bbsManager/getAllUserInfoList")
	@ResponseBody
	public Map<String,Object> getAllUserInfoList(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
	 	String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
        String realName = req.getParameter("realName");
        String userType = req.getParameter("userType");
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", Integer.parseInt(currentPage));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("realName", realName);
            paramMap.put("userType", userType);
            map = userService.getAllUsersInfoList(paramMap);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	/** 
	* @Title: disabledUsers 
	* @Description: TODO(禁用用户使用系统) 
	* @param @param req
	* @param @return     
	* @return int    返回类型 
	* @throws 
	*/
	@RequestMapping("bbsManager/disabledUsers")
	@ResponseBody
	public int disabledUsers(HttpServletRequest req){
		int num = 0;
		Long userId = Long.parseLong(req.getParameter("userId"));
		try{
			Users users = new Users();
			users.setUserId(userId);
			users.setState((byte) 0);
			num = userService.updateByPrimaryKeySelective(users);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num;
	}
	
	/** 
	* @Title: enabledUsers 
	* @Description: TODO(启用用户使用系统或者激活用户的发布功能) 
	* @param @param req
	* @param @return     
	* @return int    返回类型 
	* @throws 
	*/
	@RequestMapping("bbsManager/enabledUsers")
	@ResponseBody
	public int enabledUsers(HttpServletRequest req){
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		int num = 0;
		Long userId = Long.parseLong(req.getParameter("userId"));
		Integer state = Integer.parseInt(req.getParameter("state"));
		try{
			if(state==1) {
				SystemMessage systemMessage = new SystemMessage();
				systemMessage.setreceiveId(userId);
				systemMessage.setSendId(ud.getUserId());
				systemMessage.setMessageContent("您的二手发布功能已被解冻,请遵守商城规则.");
				systemMessageService.insertSelective(systemMessage );
			}
			Users users = new Users();
			users.setUserId(userId);
			users.setState((byte) 1);
			num = userService.updateByPrimaryKeySelective(users);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num;
	}

	/** 
	 * @Title: disabledUsers 
	 * @Description: TODO(冻结用户的二手发布功能) 
	 * @param @param req
	 * @param @return     
	 * @return int    返回类型 
	 * @throws 
	 */
	@RequestMapping("bbsManager/unActivateUsersOfFunctions")
	@ResponseBody
	public int unActivateUsersOfFunctions(HttpServletRequest req){
		int num = 0;
		Long userId = Long.parseLong(req.getParameter("userId"));
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try{
			SystemMessage systemMessage = new SystemMessage();
			systemMessage.setreceiveId(userId);
			systemMessage.setSendId(ud.getUserId());
			systemMessage.setMessageContent("您的二手发布功能由于某种原因,已被冻结.您可以拨打客服电话进行解冻.");
			systemMessageService.insertSelective(systemMessage );
			Users users = new Users();
			users.setUserId(userId);
			users.setState((byte) 2);
			num = userService.updateByPrimaryKeySelective(users);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num;
	}
	
	
	/**
	 * 添加登录用户
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/addUserInfo")
	@ResponseBody
	public Map<String,Object> addUserInfo(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
//		Map<String,Object> paramMap = new HashMap<String,Object>();
		String loginName = req.getParameter("loginName");
		Long userType = Long.parseLong(req.getParameter("userType"));
		try {
			Users findUserByLoginName = userService.findUserByLoginName(loginName);
			if(ObjectUtils.isEmpty(findUserByLoginName)) {
				Users users = new Users();
				users.setLoginName(loginName);
				users.setUserName(loginName);
				users.setPassword(MD5Util.MD5("123456"));
				users.setUserType(userType);
				users.setPhone(loginName);
				users.setSchoolId((long) 1);
				int num = userService.insertSelective(users );
				map.put("num", num);
				if(userType==7) {
					map.put("msg", "您添加了一个论坛管理员账号,账号:"+loginName+",密码:123456");
				}
			}else {
				map.put("num", "0");
				map.put("msg", "该用户:"+findUserByLoginName.getLoginName()+"已经注册,请勿重复注册!");
			}
		}catch(Exception e) {
			logger.info(e.getMessage());
			e.printStackTrace();
		}
		return map;
	}
	
	
	
}
